package com.ruoyi.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.HatchetMerchantSettle;
import com.ruoyi.system.domain.OptLog;
import com.ruoyi.system.mapper.HatchetMerchantSettleMapper;
import com.ruoyi.system.mapper.OptLogMapper;
import com.ruoyi.system.service.IHatchetMerchantSettleService;
import com.ruoyi.system.vo.Aes;
import com.ruoyi.system.vo.AjaxMessage;
import com.ruoyi.system.vo.HttpClientUtil;
import com.ruoyi.system.vo.Md5Util;

/**
 * 支付记录 服务层实现
 * 
 * @author ruoyi
 * @date 2019-02-25
 */
@Service
public class HatchetMerchantSettleServiceImpl implements IHatchetMerchantSettleService 
{
	@Autowired
	private HatchetMerchantSettleMapper hatchetMerchantSettleMapper;
	@Autowired
	private OptLogMapper optLogMapper;

	/**
     * 查询支付记录信息
     * 
     * @param iD 支付记录ID
     * @return 支付记录信息
     */
    @Override
	public HatchetMerchantSettle selectHatchetMerchantSettleById(String iD)
	{
	    return hatchetMerchantSettleMapper.selectHatchetMerchantSettleById(iD);
	}
	
	/**
     * 查询支付记录列表
     * 
     * @param hatchetMerchantSettle 支付记录信息
     * @return 支付记录集合
     */
	@Override
	public List<HatchetMerchantSettle> selectHatchetMerchantSettleList(HatchetMerchantSettle hatchetMerchantSettle)
	{
	    return hatchetMerchantSettleMapper.selectHatchetMerchantSettleList(hatchetMerchantSettle);
	}

	
	@Override
	public AjaxMessage newllyPay(HatchetMerchantSettle settle, Map<String, Object> settlemap) throws Exception {
		AjaxMessage msg = new AjaxMessage();
		ResourceBundle rb = ResourceBundle.getBundle("config");
		Map<String, String> mswgMap = new HashMap<String, String>();
		//判断日志表知否有提现记录，如果存在直接返回,当天提现记录只能有一天
		OptLog log = new OptLog();
		log.setAcq("7007");
		log.setUid(settlemap.get("userId").toString());
		Map<String,Object> map = new HashMap<>();
		map.put("beginTime", DateUtils.getDate());
		map.put("endTime", DateUtils.getNextDateStr(1));
		log.setParams(map);
		List<OptLog> list = optLogMapper.selectOptLogList(log);
		if (list.size()>0) {
			msg.setIsSuccess(false);
			msg.setContent("已经付款成功！");
			return msg;
		}
		
		if (settlemap != null) {
			String money = settlemap.get("txnAmt").toString();
			mswgMap.put("agentNo", settlemap.get("agentNo").toString());
			mswgMap.put("custName", settlemap.get("custName").toString()); // 到账开户人
			mswgMap.put("custCardNo", settlemap.get("custCardNo").toString()); // 结算卡号
			mswgMap.put("txnAmt", money); // 代付金额
			mswgMap.put("issname", settlemap.get("issname").toString());// 收款行名称
			mswgMap.put("ICNO", settlemap.get("ICNO").toString());// 身份证信息
			mswgMap.put("cnaps", settlemap.get("cnaps").toString());// 联行号
			mswgMap.put("callbackurl", rb.getString("dfnotify"));// 下游回调地址
			mswgMap.put("settleNo", StringUtils.randomUUID());// 下游订单号
			if (settlemap.get("type") != null) {
				mswgMap.put("type", settlemap.get("type").toString());
			}
		}
		String url = rb.getString("dfsendurl");

		String sign = Md5Util.signData(mswgMap,
				settlemap.get("agentKey").toString().substring(0, 16));
		mswgMap.put("sign", sign);// 签名
		net.sf.json.JSONObject object = net.sf.json.JSONObject.fromObject(mswgMap);
		String value = Aes.llyEncrypt(object.toString(), settlemap.get("agentKey").toString().substring(16, 32));// 数据加密的密钥

		Map<String, String> map2 = new HashMap<String, String>();
		map2.put("agentNo", settlemap.get("agentNo").toString());
		map2.put("dataMessage", value);
		String res = HttpClientUtil.post(url, map2);
		net.sf.json.JSONObject jsonStr = net.sf.json.JSONObject.fromObject(res);
		if ("00".equals(jsonStr.get("rescode"))) {
			msg.setIsSuccess(true);
			msg.setCode("10B");
			msg.setContent("代付成功");
		} else {
			msg.setIsSuccess(false);
			msg.setContent(jsonStr.getString("resmsg"));
		}
		
		//插入提现操作日志
		log.setUid(settlemap.get("userId").toString());
		log.setPhone(settlemap.get("custCardNo").toString());
		log.setSend(map2.toString());
		log.setReturnMsg(res);
		log.setType("TX");
		log.setAcq("7007");
		optLogMapper.insertOptLog(log);
		return msg;
	}
	
}
